import frappe
from frappe.core.doctype.doctype.doctype import DocType

class DmDocType(DocType):
    def setup_fields_to_fetch(self):
        if frappe.db.db_type == "dm":
            return dm_setup_fields_to_fetch(self)
        return super().setup_fields_to_fetch()

def dm_setup_fields_to_fetch(self):
    """达梦数据库版本的setup_fields_to_fetch方法"""
    try:
        old_meta = frappe.get_meta(frappe.get_doc("DocType", self.name), cached=False)
        old_fields_to_fetch = [df.fieldname for df in old_meta.get_fields_to_fetch()]
    except frappe.DoesNotExistError:
        old_fields_to_fetch = []

    new_meta = frappe.get_meta(self, cached=False)
    self.flags.update_fields_to_fetch_queries = []
    new_fields_to_fetch = [df for df in new_meta.get_fields_to_fetch()]

    if set(old_fields_to_fetch) != {df.fieldname for df in new_fields_to_fetch}:
        for df in new_fields_to_fetch:
            if df.fieldname not in old_fields_to_fetch:
                link_fieldname, source_fieldname = df.fetch_from.split(".", 1)
                if not source_fieldname:
                    continue  # Invalid expression
                link_df = new_meta.get_field(link_fieldname)

                # 达梦数据库的UPDATE语法
                update_query = """
                    UPDATE "tab{doctype}" target 
                    SET "target"."{fieldname}" = "source"."{source_fieldname}"
                    FROM "tab{link_doctype}" source
                    WHERE "target"."{link_fieldname}" = "source"."name"
                    AND NVL("target"."{fieldname}", '') = ''
                """

                self.flags.update_fields_to_fetch_queries.append(
                    update_query.format(
                        link_doctype=link_df.options,
                        source_fieldname=source_fieldname,
                        doctype=self.name,
                        fieldname=df.fieldname,
                        link_fieldname=link_fieldname,
                    )
                ) 